﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Services.Description;
using System.Xml;
using System.CodeDom;
using IronTrishul.Discovery;

namespace IronTrishul
{
    public partial class ScriptHost : Form
    {
        const string nameSpace = "AUTO";
        const string serviceName = "Service1";
        WSDLManager m = null;
        CodeCompileUnit ccu = null;
        CodeTypeDeclaration service = null;
        CodeTypeMemberCollection operationMembers = null;
        dynamic proxy = null;

        private dynamic DynamicProxy
        {
            get { return proxy; }
            set { proxy = value; }
        }
        public ScriptHost()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://localhost:7432/Service1.svc?wsdl";//textBox1.Text;
            try
            {
                m = new WSDLManager( url,null,null );
                ccu = m.GenerateWsdl(nameSpace);
                proxy = m.GetProxy(ccu);
                //not a good search algo.. but thats I do have in my hand
                foreach (CodeTypeDeclaration t in ccu.Namespaces[0].Types)
                {
                    if (t.Name == serviceName)
                    {
                        service = t;
                        operationMembers = service.Members;
                        break;
                    }
                }
               
                comboBox1.DataSource = operationMembers;
                comboBox1.DisplayMember = "Name";
                comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;
            }
            catch(Exception es) {
                MessageBox.Show(es.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string signaTure = "No Signature found";
            if (comboBox1.SelectedIndex >= 0)
            {
                signaTure = "";
                CodeTypeMember op = (CodeTypeMember)comboBox1.SelectedItem;

                if (op is CodeMemberMethod)
                {
                    CodeMemberMethod method = (CodeMemberMethod)op;

                    MemberAttributes am = (MemberAttributes)method.Attributes;
                    //signaTure += am.ToString() + " ";
                    signaTure += "public " + " ";
                    signaTure += method.ReturnType.BaseType + " ";
                    signaTure += method.Name + "( ";
                    for (int i=0; i< method.Parameters.Count;i++)
                    {
                        CodeParameterDeclarationExpression p = method.Parameters[i];
                        CodeTypeReference paramterType = p.Type ;
                        signaTure += paramterType.BaseType + " ";
                        if (p.Direction != FieldDirection.In )
                        {
                            signaTure += p.Direction.ToString() + " ";
                        }
                        signaTure += p.Name + " ";
                        if (i != method.Parameters.Count)
                        {
                            signaTure += " , ";
                        }
                    }
                    signaTure += " )";
                    signaTure += Environment.NewLine + @"{";
                    for (int i=0; i< method.Parameters.Count;i++)
                    {
                        CodeParameterDeclarationExpression p = method.Parameters[i];
                        CodeTypeReference paramterType = p.Type ;
                        if (p.Direction == FieldDirection.In )
                        {
                            signaTure +=Environment.NewLine + "//" + paramterType.BaseType + " m_" + p.Name + " = null;";
                            //actiual type ?
                            CodeTypeDeclaration prm = null;
                            foreach (CodeTypeDeclaration cd in ccu.Namespaces[0].Types)
                            {
                                if (cd.Name == paramterType.BaseType && cd.IsClass)
                                {
                                    foreach (CodeTypeMember mb in cd.Members)
                                    {
                                        signaTure += Environment.NewLine + "//m_" + p.Name  + "." + mb.Name + " = null"; 
                                    }
                                }
                            }
                        }
                    }
                    signaTure += Environment.NewLine + "}";
                }
            }
            this.Text = signaTure;
            textBox2.Text = signaTure;
            textBox2.AppendText(Environment.NewLine + "================");
            textBox2.AppendText(Environment.NewLine + proxy.GetType().FullName);
        }

        private void WriteLine(string line)
        {
           // textBox2.Text += "\n" + line;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WebScript dc = new WebScript();
            dc.ShowDialog();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
